/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.coffeemaker.userinput;

import com.nanosn.coffeemaker.Application;
import com.nanosn.utils.FileUtility;
import com.sun.tools.javac.Main;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

/**
 * Handles the user defined classes.
 * @author selkhateeb
 */
public class UserClasses
{

    // <editor-fold defaultstate="collapsed" desc="Sigleton Stuff">
    private static UserClasses instance;

    public static UserClasses getInstance()
    {
        if (instance == null)
        {
            instance = new UserClasses();
        }
        return instance;
    }

    private UserClasses()
    {
        //Hides the public contructor
    }
    // </editor-fold>

    /**
     * The directory in which the user defined classes are located.
     */
    private String userApplicationDirectory;

    public String getUserApplicationDirectory()
    {
        return userApplicationDirectory;
    }

    public void setUserApplicationDirectory(String userApplicationDirectory)
    {
        this.userApplicationDirectory = userApplicationDirectory;
    }

    /**
     * Gets the list of class names available in the user application directory
     * specified by the @see setUserApplicationDirectory function.
     * @return a list containing the class names.
     * @throws java.io.FileNotFoundException
     */
    public List<String> getNameList() throws FileNotFoundException
    {
        List<File> ls = FileUtility.getFileListing(
                new File(this.getUserApplicationDirectory()));
        ArrayList<String> classNameList = new ArrayList<String>();
        for (File file : ls)
        {
            if (file.toString().endsWith(".java"))
            {
                String className = file.getName();
                className = className.replace(".java", "");
                classNameList.add(className);
            }
        }
        return classNameList;
    }


    /**
     * Compiles the classes in the user application directory.
     * @return a class loader for the compiled classes.
     * @throws java.lang.Exception
     */
    @SuppressWarnings(
    {
        "static-access", "unchecked"
    })
    public ClassLoader getClassLoader() throws Exception
    {
        com.sun.tools.javac.Main javac = new Main();
        File compiledClassesDirectory =
                new File(Application.TMP + "compiledClasses" + File.separator);

        if (!compiledClassesDirectory.exists())
        {
            compiledClassesDirectory.mkdirs();
        }

        ArrayList<String> args = new ArrayList();
        args.add("-d");
        args.add(compiledClassesDirectory.getAbsolutePath());

        List<File> b = FileUtility.getFileListing(
                new File(this.getUserApplicationDirectory()));

        for (File file : b)
        {
            args.add(file.getAbsolutePath());
        }

        int compile = javac.compile(args.toArray(new String[args.size()]));
        if (compile != 0)
        {
            throw new Exception("Compiler Error");
        }

        URL url = compiledClassesDirectory.toURL();
        URLClassLoader ucl = new URLClassLoader(new URL[]
                {
                    url
                });


        return ucl;
    }
}
